/*
 * outGuestManger.java
 *
 * Created on __DATE__, __TIME__
 */

package hotel.view;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import hotel.dao.KeFangDao;
import hotel.dao.RuZhuDao;
import hotel.model.KeFang;
import hotel.util.DbUtil;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

public class outGuestManger extends javax.swing.JInternalFrame {

	DbUtil dbUtil = new DbUtil();
	RuZhuDao householdDao = new RuZhuDao();
	KeFangDao guestRoomDao = new KeFangDao();

	public outGuestManger() {
		String name = null;
		initComponents();
		this.setLocation(200, 50);//设置窗口位置
		this.fillTable(name);//初始化调用填充表格数据方法
	}

	public void fillTable(String name) {
		DefaultTableModel dtm = (DefaultTableModel) houseHoleTable.getModel();
		dtm.setRowCount(0);
		Connection con = null;
		try {
			con = dbUtil.getCon();
			ResultSet re = householdDao.getGuestRoom(con, name);
			while (re.next()) {
				Vector v = new Vector();
				//添加每一行的数据
				v.add(re.getString("id"));
				v.add(re.getString("name"));
				v.add(re.getString("idNum"));
				v.add(re.getString("roomNum"));
				v.add(re.getString("money"));
				v.add(re.getString("checkTime"));
				v.add(re.getString("state"));
				dtm.addRow(v);
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
		
				e.printStackTrace();
			}
		}
	}

	
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jLabel1.setFont(new Font("宋体", Font.PLAIN, 22));

		name = new javax.swing.JTextField();
		serch = new javax.swing.JButton();

		serch.setFont(new Font("宋体", Font.PLAIN, 22));
		jScrollPane1 = new javax.swing.JScrollPane();
		houseHoleTable = new javax.swing.JTable();
		jPanel1 = new javax.swing.JPanel();
		jLabel2 = new javax.swing.JLabel();
	
		jLabel2.setFont(new Font("宋体", Font.PLAIN, 22));
		roomNum = new javax.swing.JTextField();
		jButton1 = new javax.swing.JButton();
		jButton1.setFont(new Font("宋体", Font.PLAIN, 22));

		jLabel3 = new javax.swing.JLabel();

		jLabel3.setFont(new Font("宋体", Font.PLAIN, 22));
		id = new javax.swing.JTextField();

		setClosable(true);
		setIconifiable(true);
		setMaximizable(true);
		setTitle("退房管理");

		jLabel1.setText("姓名");

		name.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				nameActionPerformed(evt);
			}
		});

		serch.setText("查询");
		serch.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				serchActionPerformed(evt);
			}
		});

		houseHoleTable.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] {

				}, new String[] { "编号", "姓名", "身份证", "房间号", "费用", "入住时间",
						"入住状态" }) {
			boolean[] canEdit = new boolean[] { false, false, false, false,
					false, false, false };

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});
		houseHoleTable.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mousePressed(java.awt.event.MouseEvent evt) {
				houseHoleTableMousePressed(evt);
			}
		});
		jScrollPane1.setViewportView(houseHoleTable);

		jPanel1.setBorder(javax.swing.BorderFactory
				.createTitledBorder("编辑"));

		jLabel2.setText("房间号");

		roomNum.setEditable(false);

		jButton1.setText("确定退房");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jLabel3.setText("编号");

		id.setEditable(false);
		id.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				
			}
		});

		GroupLayout jPanel1Layout = new GroupLayout(
						jPanel1);
		jPanel1Layout.setVerticalGroup(
			jPanel1Layout.createParallelGroup(Alignment.LEADING)
				.addGroup(jPanel1Layout.createSequentialGroup()
					.addContainerGap()
					.addGroup(jPanel1Layout.createParallelGroup(Alignment.BASELINE)
						.addComponent(jLabel2)
						.addComponent(roomNum, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(jLabel3)
						.addComponent(id, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
					.addComponent(jButton1)
					.addContainerGap())
		);
		jPanel1Layout.setHorizontalGroup(
			jPanel1Layout.createParallelGroup(Alignment.LEADING)
				.addGroup(jPanel1Layout.createSequentialGroup()
					.addGap(20)
					.addGroup(jPanel1Layout.createParallelGroup(Alignment.TRAILING)
						.addComponent(jButton1)
						.addGroup(jPanel1Layout.createSequentialGroup()
							.addComponent(jLabel2)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(roomNum, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)))
					.addGap(18)
					.addComponent(jLabel3)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(id, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(480, Short.MAX_VALUE))
		);
		jPanel1.setLayout(jPanel1Layout);

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								layout
										.createSequentialGroup()
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(
																layout
																		.createSequentialGroup()
																		.add(
																				28,
																				28,
																				28)
																		.add(
																				jLabel1)
																		.add(
																				32,
																				32,
																				32)
																		.add(
																				name,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																				146,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
																		.add(
																				26,
																				26,
																				26)
																		.add(
																				serch))
														.add(
																layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.add(
																				layout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.LEADING)
																						.add(
																								org.jdesktop.layout.GroupLayout.TRAILING,
																								jPanel1,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.add(
																								jScrollPane1,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								974,
																								Short.MAX_VALUE))))
										.addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(
				layout.createSequentialGroup().add(41, 41, 41).add(
						layout.createParallelGroup(
								org.jdesktop.layout.GroupLayout.BASELINE).add(
								jLabel1).add(name,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
								.add(serch)).addPreferredGap(
						org.jdesktop.layout.LayoutStyle.RELATED).add(
						jScrollPane1,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 130,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.UNRELATED).add(
								jPanel1,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(74, Short.MAX_VALUE)));

		pack();
	}
	private void houseHoleTableMousePressed(java.awt.event.MouseEvent evt) {
		int row = houseHoleTable.getSelectedRow();
		this.id.setText(houseHoleTable.getValueAt(row, 0).toString());
		this.roomNum.setText(houseHoleTable.getValueAt(row, 3).toString());
	}

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		String roomNum = this.roomNum.getText();
		int id = Integer.parseInt(this.id.getText());
		if (roomNum == null || "".equals(roomNum)) {
			JOptionPane.showMessageDialog(null, "请输入房间号");
		}
		Connection con = null;
		try {
			con = dbUtil.getCon();
			int result = householdDao.updateGuestRoom(con, id);
			if (result != 0) {
				JOptionPane.showMessageDialog(null, "退房成功");
				KeFang guestRoom = new KeFang();
				guestRoom.setNum(Integer.parseInt(roomNum));
				guestRoomDao.updateState(con, guestRoom);
				guestRoom.setState(0);
				String name = null;
				this.roomNum.setText("");
				fillTable(name);
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
			
				e.printStackTrace();
			}
		}

	}

	private void serchActionPerformed(java.awt.event.ActionEvent evt) {
		String name = this.name.getText();
		this.fillTable(name);
	}

	private void nameActionPerformed(java.awt.event.ActionEvent evt) {

	}

	
	private javax.swing.JTable houseHoleTable;
	private javax.swing.JTextField id;
	private javax.swing.JButton jButton1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTextField name;
	private javax.swing.JTextField roomNum;
	private javax.swing.JButton serch;


}